package io.github.neo.hhcache.command.zset;

import io.github.neo.hhcache.core.Command;
import io.github.neo.hhcache.core.HHCache;
import io.github.neo.hhcache.entry.Reply;

import java.util.Arrays;

/**
 * @author: hhz
 * @create: 2024/7/10
 **/
public class ZaddCommand implements Command {
    @Override
    public String name() {
        return "ZADD";
    }

    @Override
    public Reply<?> exec(HHCache cache, String[] args) {
        String key = getKey(args);
        // 权重:浮点类型，需要转换
        String[] scores = getHkeys(args);
        // 元素
        String[] vals = getHvals(args);
        return Reply.integer(cache.zadd(key, vals, toDouble(scores)));
    }

    double[] toDouble(String[] scores) {
        return Arrays.stream(scores).mapToDouble(Double::parseDouble).toArray();
    }

}
